﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Conventions;
using FluentNHibernate.Mapping;
using Webshop20.Domain.Common;

namespace Webshop20.Infrastructure.Persistence.NH.AutoMappings.Conventions.Common
{
    public class AggregateValueObjectsCascadeAllConvention : IHasOneConvention, IHasManyConvention, IReferenceConvention
    {
        public bool Accept(IOneToOnePart target)
        {
            return true;
        }

        public void Apply(IOneToOnePart target)
        {
            if(target.EntityType.BaseType != typeof(Entity))
                target.Cascade.All();
        }

        public bool Accept(IOneToManyPart target)
        {
            return true;
        }

        public void Apply(IOneToManyPart target)
        {
            //var x = target.EntityType.GetGenericArguments()[0];
            target.Cascade.AllDeleteOrphan();
        }

        public bool Accept(IManyToOnePart target)
        {
            return true;
        }

        public void Apply(IManyToOnePart target)
        {
            if(target.EntityType.BaseType.Name != typeof(Entity).Name)
                target.Cascade.All();
        }
    }
}